package 模拟真题;

/**
 * @author ShadowLim
 * @create 2022-03-24-18:22
 */

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;


public class 最少次数 {

    public static void main(String[] args) {
        boolean[] visit = new boolean[10000];
        Queue<Integer> q = new LinkedList<>();
        List<Integer> list = new ArrayList<>();
        q.offer(2021);
        visit[2021] = true;
        int cnt = 0;

        while (!q.isEmpty()) {
            int size = q.size();
            while (size-- > 0) {
                int cur = q.poll();
                if (cur == 1) {
                    System.out.println(cnt);   // 14
                    return;
                }
                if (!visit[cur + 1]) {
                    visit[cur + 1] = true;
                    q.offer(cur + 1);
                    list.add(cur + 1);
                }
                if (!visit[cur - 1]) {
                    visit[cur - 1] = true;
                    q.offer(cur - 1);
                    list.add(cur - 1);
                }
                if (cur % 2 == 0 && !visit[cur / 2]) {
                    visit[cur / 2] = true;
                    q.offer(cur / 2);
                    list.add(cur / 2);
                }
            }
            cnt++;
        }
        for (int i = 0; i < list.size(); i++) {
            System.out.print(list.get(i) + " ");
        }
    }
}

